﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


namespace DomesticPestProject
{
    public partial class frmLogin : System.Web.UI.Page
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader reader;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LblPswrdVrfy.Text = "";
                TxtBxUsrNm.Focus();
            }
        }
        void Connect()
        {
            conn = new SqlConnection();
            conn.ConnectionString = "Data Source=KAJAL-PC;Initial Catalog=DomesticPestDB;Integrated Security=True";
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
        }

        void Disconnect()
        {
            cmd.Dispose();
            conn.Dispose();
            conn.Close();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
               
                Connect();
                cmd.CommandText = "select * from Login";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string str1 = Convert.ToString(reader["UserID"]);
                  Session["UserID"] = str1;
                    string str2 = Convert.ToString(reader["Password"]);
                    if (TxtBxUsrNm.Text == str1 && TxtBxPswrd.Text == str2)
                    {

                        Response.Redirect("frmMain.aspx");
                        //LblPswrdVrfy.Text = "";
                    }
                    else
                    {
                        LblPswrdVrfy.Text = "Login Failed... please enter correct UserID or PassWord";
                    }

                }
            }
            catch (Exception ex)
            {
                if (reader != null)
                    reader.Close();
                if (conn != null)
                    conn.Close();
            }
            Disconnect();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}